﻿using CefSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoRetailsDLL.Workers.PinDuoDuo
{
    public class UploadFileDialogHandler : IDialogHandler
    {
        public static List<string> file_paht_list = new List<string>();
        public bool OnFileDialog(IWebBrowser chromiumWebBrowser, IBrowser browser, CefFileDialogMode mode, string title, string defaultFilePath, List<string> acceptFilters, IFileDialogCallback callback)
        {
            callback.Continue(file_paht_list); //new List<string> { @"E:\谷歌下载\1.jpeg", @"E:\谷歌下载\2.jpeg", @"E:\谷歌下载\3.jpeg" }
            return true;
        }

        //public bool OnFileDialog(IWebBrowser chromiumWebBrowser, IBrowser browser, CefFileDialogMode mode, string title, string defaultFilePath, IReadOnlyCollection<string> acceptFilters, IReadOnlyCollection<string> acceptExtensions, IReadOnlyCollection<string> acceptDescriptions, IFileDialogCallback callback)
        //{
        //    callback.Continue(file_paht_list); //new List<string> { @"E:\谷歌下载\1.jpeg", @"E:\谷歌下载\2.jpeg", @"E:\谷歌下载\3.jpeg" }
        //    return true;
        //}

        public bool OnJSDialog(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, CefJsDialogType dialogType, string messageText, string defaultPromptText, IJsDialogCallback callback, ref bool suppressMessage)
        {
            //suppressMessage = true;
            callback.Continue(true);
            return true;
        }
        //public bool OnFileDialog(IWebBrowser chromiumWebBrowser, IBrowser browser, CefFileDialogMode mode, CefFileDialogFlags flags, string title, string defaultFilePath, List<string> acceptFilters, int selectedAcceptFilter, IFileDialogCallback callback)
        //{
        //    callback.Continue(selectedAcceptFilter, new List<string> { @"E:\softwaredir\img\4.mp4" });
        //    return true;
        //}
    }

    public class JsDialogHandler : IJsDialogHandler
    {
        public bool OnBeforeUnloadDialog(IWebBrowser chromiumWebBrowser, IBrowser browser, string messageText, bool isReload, IJsDialogCallback callback)
        {
            callback.Continue(true, string.Empty);
            return true;
        }

        public void OnDialogClosed(IWebBrowser chromiumWebBrowser, IBrowser browser)
        {
        }

        public bool OnJSDialog(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, CefJsDialogType dialogType, string messageText, string defaultPromptText, IJsDialogCallback callback, ref bool suppressMessage)
        {
            switch (dialogType)
            {
                case CefSharp.CefJsDialogType.Alert:
                    suppressMessage = true;
                    return false;
                case CefSharp.CefJsDialogType.Confirm:
                    var dr = MessageBox.Show(messageText, "提示", MessageBoxButtons.YesNo);
                    if (dr == DialogResult.Yes)
                    {
                        callback.Continue(true, string.Empty);
                        suppressMessage = false;
                        return true;
                    }
                    else
                    {
                        callback.Continue(false, string.Empty);
                        suppressMessage = false;
                        return true;
                    }
                    break;
                case CefSharp.CefJsDialogType.Prompt:
                    break;
                default:
                    break;
            }
            return false;
            //switch (dialogType)
            //{
            //    case CefSharp.CefJsDialogType.Alert:
            //        MessageBox.Show(messageText, "提示");
            //        suppressMessage = true;
            //        return false;
            //    case CefSharp.CefJsDialogType.Confirm:
            //        var dr = MessageBox.Show(messageText, "提示", MessageBoxButtons.YesNo);
            //        if (dr == DialogResult.Yes)
            //        {
            //            callback.Continue(true, string.Empty);
            //            suppressMessage = false;
            //            return true;
            //        }
            //        else
            //        {
            //            callback.Continue(false, string.Empty);
            //            suppressMessage = false;
            //            return true;
            //        }
            //        break;
            //    case CefSharp.CefJsDialogType.Prompt:
            //        MessageBox.Show("系统不支持prompt形式的提示框", "提示");
            //        break;
            //    default:
            //        break;
            //}
            //return false;
        }

        public void OnResetDialogState(IWebBrowser chromiumWebBrowser, IBrowser browser)
        {
        }
    }
}
